package com.hoshiicloud.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hoshiicloud.common.query.OmnipotentParam;
import com.hoshiicloud.common.query.OmnipotentWrapper;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.update.UpdateBatchParam;
import com.hoshiicloud.goods.entity.goods.GoodsGallery;
import com.hoshiicloud.goods.param.goods.GoodsGalleryQueryParam;
import com.hoshiicloud.goods.service.GoodsGalleryService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author linjx on 2019/8/13
 */
@RestController
@RequestMapping("/goods/goods-gallery")
public class GoodsGalleryController {

    @Autowired
    private GoodsGalleryService goodsGalleryService;

    @PostMapping("/queryGoodsGallery")
    @ApiOperation(value = "商品图片资源关联表查询")
    public Response queryGoodsGallery(@RequestBody GoodsGalleryQueryParam param) {
        QueryWrapper<GoodsGallery> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(param.getId() != null, GoodsGallery::getId, param.getId())
                .eq(param.getExtSpuId() != null, GoodsGallery::getExtSpuId, param.getExtSpuId())
                .eq(param.getBaseSpuId() != null, GoodsGallery::getBaseSpuId, param.getBaseSpuId())
                .eq(param.getGalleryId() != null, GoodsGallery::getGalleryId, param.getGalleryId())
                .eq(param.getWeight() != null, GoodsGallery::getWeight, param.getWeight());
        String key = param.toString();
        return Response.data(goodsGalleryService.listGoodsGalleryByWrapper(wrapper, key));
    }

    @PostMapping("/saveBatch")
    @ApiOperation(value = "批量保存商品图片资源信息")
    public Response saveGoodsGalleryBatch(@RequestBody List<GoodsGallery> galleryList) {
        return Response.data(goodsGalleryService.saveGoodsGalleryBatch(galleryList));
    }

    @PostMapping("/listBySpuIds")
    @ApiOperation(value = "根据spuId集合查询图片资源关联")
    public Response<List<GoodsGallery>> listBySpuIds(@RequestBody List<Long> spuIds){
        return Response.data(goodsGalleryService.listBySpuIds(spuIds));
    }

    @GetMapping("/deleteBatchs/{extSpuId}")
    @ApiOperation(value = "根据ExtSpuId 进行批量删除")
    public Response deleteBatchs(@PathVariable("extSpuId") Long extSpuId) {
        return Response.data(goodsGalleryService.deleteBatchs(extSpuId));
    }

    @DeleteMapping("/removeBatch")
    @ApiOperation(value = "批量删除: 物理删除")
    public Response removeBatch(@RequestBody List<OmnipotentWrapper> param) {
        return Response.data(goodsGalleryService.removeBatch(param));
    }

    @PostMapping("/updateBatch")
    @ApiOperation(value = "批量更新")
    public Response updateBatch(@RequestBody OmnipotentParam<GoodsGallery> param) {
        return Response.data(goodsGalleryService.updateBatch(param));
    }

}
